package com.cloud.jdkTest;


/**
 * synchronized是否保证可见性
 * synchronized会保证对进入同一个监视器的线程保证可见性。
 * 比如线程th1修改了变量，退出监视器之前，会把修改变量值v1刷新的主内存当中；
 * 当线程t2进入这个监视器时，如果有某个处理器缓存了变量v1，首先缓存失效，然后必须重主内存重新加载变量值v1
 * http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#incorrectlySync
 */
public class SyncVisibilityPro {
    public static Integer max = 100;

    public static Object object = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; ++i) {
            threads[i] = new Thread(() -> {
                while (true) {
                    if (max == null) {
                        //这句话永远不会被打印
                        System.out.println("I have feel the change of the value");
                        break;
                    }
                }
            });
            threads[i].start();
        }

        Thread.sleep(100);

        synchronized (object) {
            max = null;
            System.out.println("I changed the value of the max");
        }

    }
}
